package visualcbir.decorators;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import visualcbir.util.VSUtils;
import visualcbir.PnlView;
import visualcbir.View;
import visualcbir.ViewComposed;
import visualcbir.node.ModelNode;
import visualcbir.node.ViewNode;

/**
 *
 * @author sheila
 */
public class FunVideoOnDemand extends Functionality implements ControllerListener{
    private JDialog dlg;
    private JPanel pnl;
    private Component visualComponent;
    private Player player;

    private JLabel lblName;
    private JLabel lblId;

    ImageIcon oImageIcon=new ImageIcon();
    MouseAdapterFun maf=new  MouseAdapterFun();

    public FunVideoOnDemand(View oView, PnlView poPnlView) {
        super(oView, poPnlView);
        initComponents();
        this.oPnlView.addMouseListener(maf);
    }

    public void initComponents(){
        this.pnl=new JPanel();
        this.dlg=new JDialog();
        try{
            player = Manager.createPlayer(new MediaLocator("/home/sheila/NetBeansProjects/clusteringset-video/VideoBox/Level1/Video/v59.mpg"));
            player.addControllerListener(this);
            player.start();
        }
        catch(Exception e){       e.printStackTrace();        }


        this.lblName=new JLabel();
        this.lblId=new JLabel();
        JPanel pnltmp = new JPanel();
        pnltmp.setLayout(new BorderLayout());
        pnltmp.add(lblId, BorderLayout.NORTH);
        pnltmp.add(lblName, BorderLayout.SOUTH);
        
        pnltmp.setBackground(Color.white);

        this.pnl.setLayout(new BorderLayout());
        this.pnl.setBackground(Color.white);
        this.pnl.add(new JLabel(oImageIcon), BorderLayout.NORTH);
        this.pnl.add(pnltmp, BorderLayout.SOUTH);
        this.dlg.add(pnl);

    }
    public void removeAll(){
        this.oPnlView.removeMouseListener(maf);
    }

    @Override
   public void controllerUpdate(ControllerEvent c)
    {
            if(player == null)
                    return;

            if(c instanceof RealizeCompleteEvent)
            {
                    if((visualComponent = player.getVisualComponent()) != null)
                            pnl.add(visualComponent);
            }
    }


    public class MouseAdapterFun extends MouseAdapter{
        public MouseAdapterFun(){
            super();
        }

        @Override
        public void mousePressed(MouseEvent event) {
             ViewNode nearestViewNode = ((ViewComposed)oView).findNode(event.getX(),event.getY());
             //if( ((ViewComposed)oView).getView(origem) instanceof ViewNode && origem !=-1){
             if( nearestViewNode != null){
                 ModelNode model= (ModelNode)nearestViewNode.getModel();
                 lblId.setText(" * ID: "+model.getId());
                 //14agost lblName.setText( " * Name: "+model.getStrUrl().substring(model.getStrUrl().lastIndexOf("/")+1) ); //+" - Rank:"+model.getiRank() //"Relevance"+model.getdRelevance()

                 Image img=VSUtils.readImage(model.getStrPath() ); // .getStrUrl()
                 oImageIcon.setImage( img );                 
                 dlg.setLocation(nearestViewNode.getCenterPoint());
                 dlg.setSize(img.getWidth(null), img.getHeight(null)+60);
                 dlg.show();
                 //System.out.println("--Mouse pressed. existing view");
            }
            //else System.out.println("--Mouse pressed. nearest node foi null.");

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println("DetailsonDemand: exited");
            dlg.hide();
        }
    }    

}
